package edu.gdin.ecommerce.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

import edu.gdin.ecommerce.dao.UserDAO;
import edu.gdin.ecommerce.model.Role;
import edu.gdin.ecommerce.model.User;
import edu.gdin.ecommerce.service.IUserService;
import edu.gdin.ecommerce.util.DateUtil;
import edu.gdin.ecommerce.util.OperLogUtil;
import edu.gdin.ecommerce.util.Pager;

@Service("userService")
public class UserService implements IUserService {

	private UserDAO userDAO;

	public UserDAO getUserDAO() {
		return userDAO;
	}
	@Resource
	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}
	
	/**
	 * 保存登录用户信息
	 * @return
	 */
	public User login() {
		Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		String account = "";
		if(principal instanceof UserDetails) {
			account = ((UserDetails) principal).getUsername();
		} else {
			account = principal.toString();
		}
		
		User user = this.userDAO.getUserByAccount(account);
		user.setLastTime(new Date());
		this.userDAO.update(user);
		OperLogUtil.log(this.userDAO, "1", "11", "", account);
		
		return user;
	}
	
	/**
	 * 修改密码
	 * @param user
	 * @param newPwd
	 * @return
	 */
	public boolean changePwd(User user, String newPwd) {
		try {
			user.setPassword(newPwd);
			this.userDAO.update(user);
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}
	
	/**
	 * 根据账号查询用户
	 * @param account
	 * @param currentPage
	 * @return
	 */
	public Pager query(String account, int currentPage) {
		
		account = account.trim();
		
		String hql = "from User u where u.account like '%" + account + "%'";
		Pager pager = this.userDAO.getPage(hql, currentPage);
		
		return pager;
		
	}
	
	/**
	 * 判断是否存在同名账号
	 * @param user
	 * @return
	 */
	public boolean isExist(User user) {
		return this.userDAO.isExist(User.class, "account", user.getAccount());
	}
	
	/**
	 * 添加用户
	 * @param user
	 */
	public void addUser(User user) {
		this.userDAO.insert(user);
	}
	
	/**
	 * 根据账号获取用户信息
	 * @param account
	 * @return
	 */
	public User getUserUserByAccount(String account) {
		User user = this.userDAO.getUserByAccount(account);
		// 避免懒加载问题!
		System.out.println("selected User's roles' size:" + user.getRoles().size());
		return user;
	}
	
	/**
	 * 修改用户
	 * @param user
	 * @param roles
	 * @param roleIds
	 */
	public void editUser(User user, List<Role> roles, List<Integer> roleIds) {
		User u = this.userDAO.getUserByAccount(user.getAccount());
		u.setUsername(user.getUsername());
		u.setMobile(user.getMobile());
		u.setRemarks(user.getRemarks());
		u.setLastTime(DateUtil.now());
		
		List<Role> selectedRoles = new ArrayList<Role>();
		for (Integer i : roleIds) {
			for (Role r : roles) {
				if (r.getRoleId().equals(i)) {
					selectedRoles.add(r);
				}
			}
		}
		u.setRoles(selectedRoles);
		this.userDAO.update(u);
	}
	
	/**
	 * 改变用户状态 - 启用/停用
	 * @param user
	 */
	public void changeUserStatus(User user) {
		User u = this.userDAO.getUserByAccount(user.getAccount());
		
		if(u.getStatus() == 0) { // 0=正常 1=停用
			u.setStatus(1); 
		} else {
			u.setStatus(0);
		} 
		this.userDAO.update(u);
	}
	
	
}
